package com.woniu.commentorder.rabbitConsumer;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hc.entity.Order;
import com.hc.entity.PayRecord;
import com.hc.entity.User;
import com.hc.utils.ResponseEntity;
import com.hc.vo.UtilClass;
import com.rabbitmq.client.Channel;
import com.woniu.commentorder.mapper.PayRecordMapper;
import com.woniu.commentorder.service.GoodService;
import com.woniu.commentorder.service.PayRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InvalidClassException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class OrderConsumer {
    @Autowired
    private GoodService goodService;
    @Autowired
    private PayRecordMapper payRecordMapper;
    @Autowired
    private PayRecordService payRecordService;


    @RabbitListener(queues = "goodsqueue")
    public String decstock(String utilClassstr, Message message, Channel channel) throws IOException {
        JSONObject jsonObject = JSON.parseObject(utilClassstr);
        UtilClass utilClass = new UtilClass();
        utilClass.setId(jsonObject.getInteger("id"));
        utilClass.setNum(jsonObject.getInteger("num"));
        log.info("开始操作");
        if (message.getMessageProperties().getReceivedRoutingKey().startsWith("decrease")) {
            log.info("减少库存");
//            if (2 > 1) {
//                throw new InvalidClassException("测试跑得异常");
//            }
            goodService.destock(utilClass.getNum(), utilClass.getId());
        } else {
            log.info("增加库存");
            goodService.instock(utilClass.getNum(), utilClass.getId());
        }
        System.out.println("str:" + utilClassstr);
        System.out.println("message:" + message);
        System.out.println("channel:" + channel);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        return "库存操作成功";
    }
//    @RabbitListener(queues = "payrecordqueue")
//    public String unPayOrder(String payjson, Message message, Channel channel) throws IOException {
//        log.info("订单处理");
//        JSONObject jsonObject = JSON.parseObject(payjson);
//        PayRecord payRecord=new PayRecord();
//        User user=new User();
//        user.setId(jsonObject.getJSONObject("user").getInteger("id"));
//        payRecord.setUser(user);
//        Order order=new Order();
//        order.setId(jsonObject.getJSONObject("order").getInteger("id"));
//        payRecord.setOrder(order);
//        payRecord.setFee(jsonObject.getBigDecimal("fee"));
//        payRecord.setDateTime(new Date());
//        payRecordService.add(payRecord);
//        System.out.println("str:" + payjson);
//        System.out.println("message:" + message);
//        System.out.println("channel:" + channel);
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
////        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
//        log.info("接下来进入死信队列");
//        return "订单添加处理成功";
//    }
    @RabbitListener(queues = "deadqueue")
    public void deadOrder(String payjson, Message message, Channel channel) throws IOException {
        System.out.println("message:" + message);
        log.info("取到的消息id为"+Integer.parseInt(payjson));
        log.info("进入死信队列");
        payRecordMapper.delete(Integer.parseInt(payjson));
        log.info("成功删除订单");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
